import os
import pathlib
import shutil
import tkinter as tk
from tkinter import filedialog, ttk, messagebox
import yaml

class AdvancedFileCopyApp:
    def __init__(self, root):
        self.root = root
        self.root.title("高级文件/文件夹复制工具")
        self.root.geometry("800x650")
        self.root.resizable(True, True)
        
        # 存储选择的路径
        self.source_files = []
        self.source_folders = []
        self.target_folders = []
        
        # 创建界面
        self.create_widgets()
        
        # 初始化路径
        self.current_path = str(pathlib.Path(__file__).parent.absolute().resolve())

    def create_widgets(self):
        # 主框架
        main_frame = ttk.Frame(self.root, padding="10")
        main_frame.pack(fill=tk.BOTH, expand=True)
        
        # 源文件选择区域
        ttk.Label(main_frame, text="一、选择需要复制的文件:").pack(anchor=tk.W, pady=(10, 5))
        file_frame = ttk.Frame(main_frame)
        file_frame.pack(fill=tk.X, pady=(0, 10))
        
        ttk.Button(file_frame, text="添加文件...", command=self.add_source_files).pack(side=tk.LEFT, padx=(0, 5))
        ttk.Button(file_frame, text="清除文件", command=self.clear_source_files).pack(side=tk.LEFT)
        
        # 源文件列表
        self.file_listbox = tk.Listbox(main_frame, height=4, selectmode=tk.EXTENDED)
        self.file_listbox.pack(fill=tk.X, pady=(0, 10))
        ttk.Button(main_frame, text="删除选中文件", command=self.remove_selected_files).pack(anchor=tk.W, pady=(0, 10))
        
        # 源文件夹选择区域
        ttk.Label(main_frame, text="二、选择需要复制的文件夹:").pack(anchor=tk.W, pady=(10, 5))
        folder_frame = ttk.Frame(main_frame)
        folder_frame.pack(fill=tk.X, pady=(0, 10))
        
        ttk.Button(folder_frame, text="添加文件夹...", command=self.add_source_folders).pack(side=tk.LEFT, padx=(0, 5))
        ttk.Button(folder_frame, text="清除文件夹", command=self.clear_source_folders).pack(side=tk.LEFT)
        
        # 源文件夹列表
        self.folder_listbox = tk.Listbox(main_frame, height=4, selectmode=tk.EXTENDED)
        self.folder_listbox.pack(fill=tk.X, pady=(0, 10))
        ttk.Button(main_frame, text="删除选中文件夹", command=self.remove_selected_folders).pack(anchor=tk.W, pady=(0, 10))
        
        # 目标文件夹选择区域
        ttk.Label(main_frame, text="三、选择目标文件夹(可多选):").pack(anchor=tk.W, pady=(10, 5))
        target_frame = ttk.Frame(main_frame)
        target_frame.pack(fill=tk.X, pady=(0, 10))
        
        ttk.Button(target_frame, text="添加目标文件夹...", command=self.add_target_folders).pack(side=tk.LEFT, padx=(0, 5))
        ttk.Button(target_frame, text="清除目标文件夹", command=self.clear_target_folders).pack(side=tk.LEFT)
        
        # 目标文件夹列表
        self.target_listbox = tk.Listbox(main_frame, height=4, selectmode=tk.EXTENDED)
        self.target_listbox.pack(fill=tk.X, pady=(0, 10))
        ttk.Button(main_frame, text="删除选中目标文件夹", command=self.remove_selected_targets).pack(anchor=tk.W, pady=(0, 10))
        
        # 操作按钮
        btn_frame = ttk.Frame(main_frame)
        btn_frame.pack(fill=tk.X, pady=(10, 10))
        
        ttk.Button(btn_frame, text="开始复制", command=self.start_copy).pack(side=tk.LEFT, padx=(0, 10))
        ttk.Button(btn_frame, text="清除日志", command=self.clear_log).pack(side=tk.LEFT)
        
        # 进度条
        self.progress_var = tk.DoubleVar()
        ttk.Label(main_frame, text="复制进度:").pack(anchor=tk.W, pady=(10, 5))
        self.progress_bar = ttk.Progressbar(main_frame, variable=self.progress_var, maximum=100)
        self.progress_bar.pack(fill=tk.X, pady=(0, 10))
        
        # 日志区域
        ttk.Label(main_frame, text="操作日志:").pack(anchor=tk.W, pady=(10, 5))
        log_frame = ttk.Frame(main_frame)
        log_frame.pack(fill=tk.BOTH, expand=True)
        
        scrollbar = ttk.Scrollbar(log_frame)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        
        self.log_text = tk.Text(log_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set, state=tk.DISABLED)
        self.log_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        scrollbar.config(command=self.log_text.yview)

    # 源文件相关操作
    def add_source_files(self):
        files = filedialog.askopenfilenames(
            initialdir=self.current_path,
            title="选择要复制的文件"
        )
        if files:
            for file in files:
                if file not in self.source_files:
                    self.source_files.append(file)
                    self.file_listbox.insert(tk.END, file)

    def clear_source_files(self):
        self.source_files.clear()
        self.file_listbox.delete(0, tk.END)

    def remove_selected_files(self):
        selected = sorted(self.file_listbox.curselection(), reverse=True)
        for i in selected:
            del self.source_files[i]
            self.file_listbox.delete(i)

    # 源文件夹相关操作
    def add_source_folders(self):
        folder = filedialog.askdirectory(
            initialdir=self.current_path,
            title="选择要复制的文件夹"
        )
        if folder and folder not in self.source_folders:
            self.source_folders.append(folder)
            self.folder_listbox.insert(tk.END, folder)

    def clear_source_folders(self):
        self.source_folders.clear()
        self.folder_listbox.delete(0, tk.END)

    def remove_selected_folders(self):
        selected = sorted(self.folder_listbox.curselection(), reverse=True)
        for i in selected:
            del self.source_folders[i]
            self.folder_listbox.delete(i)

    # 目标文件夹相关操作
    def add_target_folders(self):
        folder = filedialog.askdirectory(
            initialdir=self.current_path,
            title="选择目标文件夹"
        )
        if folder and folder not in self.target_folders:
            self.target_folders.append(folder)
            self.target_listbox.insert(tk.END, folder)

    def clear_target_folders(self):
        self.target_folders.clear()
        self.target_listbox.delete(0, tk.END)

    def remove_selected_targets(self):
        selected = sorted(self.target_listbox.curselection(), reverse=True)
        for i in selected:
            del self.target_folders[i]
            self.target_listbox.delete(i)

    # 日志和进度条操作
    def log(self, message):
        self.log_text.config(state=tk.NORMAL)
        self.log_text.insert(tk.END, message + "\n")
        self.log_text.see(tk.END)
        self.log_text.config(state=tk.DISABLED)
        self.root.update_idletasks()

    def clear_log(self):
        self.log_text.config(state=tk.NORMAL)
        self.log_text.delete(1.0, tk.END)
        self.log_text.config(state=tk.DISABLED)

    # 核心复制功能
    def start_copy(self):
        # 验证选择
        if not self.source_files and not self.source_folders:
            messagebox.showerror("错误", "请至少选择一个文件或文件夹进行复制！")
            return
            
        if not self.target_folders:
            messagebox.showerror("错误", "请至少选择一个目标文件夹！")
            return
        
        # 准备所有要复制的项目
        items_to_copy = []
        items_to_copy.extend(self.source_files)
        items_to_copy.extend(self.source_folders)
        
        total_items = len(items_to_copy)
        total_targets = len(self.target_folders)
        total_operations = total_items * total_targets
        completed_operations = 0
        
        self.log("===== 开始复制操作 =====")
        self.log(f"待复制项: {total_items} 个（文件: {len(self.source_files)}, 文件夹: {len(self.source_folders)}）")
        self.log(f"目标文件夹: {total_targets} 个")
        
        for target_idx, target_folder in enumerate(self.target_folders, 1):
            self.log(f"\n处理目标文件夹 {target_idx}/{total_targets}: {target_folder}")
            
            for item_idx, item in enumerate(items_to_copy, 1):
                item_name = os.path.basename(item)
                target_path = os.path.join(target_folder, item_name)
                
                try:
                    # 确保目标目录存在
                    os.makedirs(os.path.dirname(target_path), exist_ok=True)
                    
                    if os.path.isfile(item):
                        # 处理YAML文件
                        if item_name.lower().endswith(('.yaml', '.yml')):
                            with open(item, 'r', encoding='utf-8') as f:
                                content = f.read()
                            with open(target_path, 'w', encoding='utf-8') as f:
                                f.write(content)
                            self.log(f"  YAML文件 {item_idx}/{total_items}: {item_name} [成功]")
                        else:
                            shutil.copy2(item, target_path)
                            self.log(f"  文件 {item_idx}/{total_items}: {item_name} [成功]")
                    elif os.path.isdir(item):
                        if not os.path.exists(target_path):
                            shutil.copytree(item, target_path)
                            self.log(f"  目录 {item_idx}/{total_items}: {item_name} [成功]")
                        else:
                            self.log(f"  目录 {item_idx}/{total_items}: {item_name} [已存在，跳过]")
                    else:
                        self.log(f"  未知类型 {item_idx}/{total_items}: {item_name} [跳过]")
                        
                except Exception as e:
                    self.log(f"  错误 {item_idx}/{total_items}: {item_name} → {str(e)}")
                
                # 更新进度
                completed_operations += 1
                progress = (completed_operations / total_operations) * 100
                self.progress_var.set(progress)
                self.root.update_idletasks()
        
        self.log("\n===== 操作完成 =====")
        messagebox.showinfo("完成", "所有复制操作已完成！")
        self.progress_var.set(100)

if __name__ == "__main__":
    root = tk.Tk()
    app = AdvancedFileCopyApp(root)
    root.mainloop()